package com.example.bbfindhouse.Activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bbfindhouse.R;

public class woyaomaifangActivity extends AppCompatActivity {

        private Spinner spinnerCity;
        private EditText editTextCommunity, editTextAddress, editTextRent, editTextName, editTextContact;
        private Button buttonSubmit;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_woyaomaifang);

            spinnerCity = findViewById(R.id.spinner_city);
            editTextCommunity = findViewById(R.id.editText_community);
            editTextAddress = findViewById(R.id.editText_address);
            editTextRent = findViewById(R.id.editText_rent);
            editTextName = findViewById(R.id.editText_name);
            editTextContact = findViewById(R.id.editText_contact);
            buttonSubmit = findViewById(R.id.button_submit);

            ArrayAdapter<CharSequence> adapterCity = ArrayAdapter.createFromResource(this,
                    R.array.cities, android.R.layout.simple_spinner_item);
            adapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerCity.setAdapter(adapterCity);

            ArrayAdapter<CharSequence> adapterRentType = ArrayAdapter.createFromResource(this,
                    R.array.rent_types, android.R.layout.simple_spinner_item);
            adapterRentType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            buttonSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (validateFields()) {
                        showSubmissionResult(true);
                    } else {
                        showSubmissionResult(false);
                    }
                }
            });
        }

        private boolean validateFields() {
            String city = spinnerCity.getSelectedItem().toString();
            String community = editTextCommunity.getText().toString();
            String address = editTextAddress.getText().toString();
            String rent = editTextRent.getText().toString();
            String name = editTextName.getText().toString();
            String contact = editTextContact.getText().toString();

            if (city.isEmpty() || community.isEmpty() || address.isEmpty() || rent.isEmpty() ||
                    name.isEmpty() || contact.isEmpty()) {
                return false;
            }
            return true;
        }

        private void showSubmissionResult(boolean isSuccess) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提交结果");
            if (isSuccess) {
                builder.setMessage("提交成功！");
            } else {
                builder.setMessage("提交失败，请检查必填项是否填写完整！");
            }
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.show();
        }
    }